/*
 * Created by kopper on 31.01.15 18:45
 * (C) Copyright 2015 kopperek@gmail.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package app.android.kopper.foramemo.util;

import java.util.Date;

public class WordGroupDef {

    public static WordGroupDef getAllWords() {
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 ",
                new String[]{});
    }

    public static WordGroupDef getNewWords() {
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + " is null and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE + " is null ",
                new String[]{});
    }

    public static WordGroupDef getNewMistakenWords() {
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + " is null and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE + " is not null ",
                new String[]{});
    }

    public static WordGroupDef getNewPassedWords() {
        String now=String.valueOf(ForaMemoDbHelper.normalizeDate(new Date()));
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + ">? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_LAST_ANSWER + "=? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_FIRST_OCCURRENCE + "=?",
                new String[]{now,now,now});
    }

    public static WordGroupDef getRepWords() {
        String now=String.valueOf(ForaMemoDbHelper.normalizeDate(new Date()));
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + "<=? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE + " is null ",
                new String[]{now});
    }

    public static WordGroupDef getRepMistakenWords() {
        String now=String.valueOf(ForaMemoDbHelper.normalizeDate(new Date()));
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + "<=? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE + " is not null ",
                new String[]{now});
    }

    public static WordGroupDef getRepPassedWords() {
        String now=String.valueOf(ForaMemoDbHelper.normalizeDate(new Date()));
        return new WordGroupDef(
                ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE + " =1 and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE + ">? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_LAST_ANSWER + "=? and " + ForaMemoDbHelper.FeedEntry.COLUMN_NAME_FIRST_OCCURRENCE + "<?",
                new String[]{now,now,now});
    }


    private final String query;
    private final String[] queryParams;

    private WordGroupDef(String query, String[] queryParams) {
        this.query=query;
        this.queryParams=queryParams;
    }

    public long getCount(ForaMemoDbHelper dbHelper) {
        return dbHelper.selectWordsCount(dbHelper.getReadableDatabase(),query,queryParams);
    }

    public java.util.Set<Word> getWords(ForaMemoDbHelper dbHelper, Long limit) {
        return dbHelper.selectWords(dbHelper.getReadableDatabase(), query, limit, queryParams);
    }
}
